package com.stedu.service.impl;

import com.stedu.bean.Borrow;
import com.stedu.mapper.BorrowMapper;
import com.stedu.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class BorrowServiceImpl implements BorrowService {
    @Autowired
    private BorrowMapper borrowMapper;

    @Cacheable(value = "book_back", key = "'borrowAll'", unless = "#result == null || #result.size() == 0")
    @Override
    public List<Borrow> selectAll() {
        return borrowMapper.selectAll();
    }

    @CacheEvict(value = "book_back", key = "'borrowAll'")
    @Override
    public boolean insertBo(Borrow borrow) {
        return borrowMapper.insertBo(borrow);
    }

    @CacheEvict(value = "book_back", key = "'borrowAll'")
    @Override
    public boolean deleteBo(Integer boid) {
        return borrowMapper.deleteBo(boid) == 1;
    }

    @CacheEvict(value = "book_back", key = "'borrowAll'")
    @Override
    public boolean updateBo(Borrow borrow) {
        borrow.setBostate(0);
        BigDecimal bigDecimal = new BigDecimal(0);
        borrow.setBodeposit(bigDecimal);
        return borrowMapper.updateBo(borrow) == 1;
    }

    @Override
    @CacheEvict(value = "book_back", key = "'borrowAll'")
    public boolean updateBo1(Borrow borrow) {
        borrow.setBostate(borrow.getBostate());
        return  borrowMapper.updateBo(borrow) == 1;
    }

    @Override
    public Borrow selectById(Integer boid) {
        return borrowMapper.selectById(boid);
    }
}
